package com.lrj.datastream.transform.basic;

import com.lrj.datastream.domain.ClickEvent;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author lrj
 * @date 2022/3/28 15:47
 */
public class TestFlatMap {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<ClickEvent> stream = env.fromElements(
                new ClickEvent("Mary", "./home", 1000L),
                new ClickEvent("Blob", "./cart", 2100L),
                new ClickEvent("Mary", "./product?id=100", 3000L));
        stream.flatMap(new MyFlatMap()).print();
        stream.flatMap((ClickEvent v, Collector<String> c) -> {
            String user = v.getUser();
            String url = v.getUrl();
            c.collect(user);
            c.collect("=>");
            c.collect(url);
        }).returns(new TypeHint<String>() {
        }).print();
        env.execute(TestFilter.class.getName());
    }

    private static class MyFlatMap implements FlatMapFunction<ClickEvent, String> {
        @Override
        public void flatMap(ClickEvent v, Collector<String> c) throws Exception {
            String user = v.getUser();
            String url = v.getUrl();
            c.collect(user + "==>" + url);
        }
    }
}
